package com.yunche.yunchebusiness.utils

import android.annotation.SuppressLint
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.base.BaseApp
import es.dmoral.toasty.Toasty

/**
 * Create by Crcker on 2018/12/20 11:06
 * Email: Crcker@163.com
 */


object ToastyUtils {
    @SuppressLint("CheckResult")
    fun showError(msg: String) {
        Toasty.error(BaseApp.mContext!!, msg, Toast.LENGTH_SHORT, true).show()
    }

    fun showSuccess(msg: String) {
        Toasty.success(BaseApp.mContext!!, msg, Toast.LENGTH_SHORT, true).show()
    }

    fun showInfo(msg: String) {
        Toasty.info(BaseApp.mContext!!, msg, Toast.LENGTH_SHORT, true).show()
    }

    fun showWarning(msg: String) {
        Toasty.warning(BaseApp.mContext!!, msg, Toast.LENGTH_SHORT, true).show()
    }

    fun showNormal(msg: String) {
        Toasty.normal(BaseApp.mContext!!, msg, Toast.LENGTH_SHORT).show()
    }

    fun show(msg: String) {
        makeTextToast(BaseApp.mContext!!, msg, Toast.LENGTH_LONG).show()
    }

    private fun makeTextToast(context: Context, text: CharSequence, duration: Int): MyToast {
        val result = MyToast(context)
        val inflate = context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val v = inflate.inflate(R.layout.view_toast, null)
        val tv = v.findViewById<View>(android.R.id.message) as TextView
        tv.text = text
        result.view = v
        // setGravity方法用于设置位置，此处为垂直居中
        result.setGravity(Gravity.CENTER_VERTICAL, 0, 0)
        result.duration = duration
        return result
    }
}